<template>
  <view v-if="loginFlag" class="collect-page">
    <view class="collect-tab">
      <view class="tab-item" :class="{ active: tabIndex === 2 }" @click="hancldChange(2)">
        专栏
      </view>
      <view class="tab-item tab-right" :class="{ active: tabIndex === 1 }" @click="hancldChange(1)">
        课程
      </view>
    </view>

    <scroll-view v-if="collectList.length" scroll-y="auto" class="collect-list" @scrolltolower="scrolltolower">
      <view v-for="item in collectList" :key="item.collectionId" class="collect-item" @click="toDetail(item)">
        <image class="item-img" mode="aspectFill" :src="`https://shuyutk.oss-cn-qingdao.aliyuncs.com${item.coverPic}`" />
        <text class="item-title">
          {{ item.courseDetailTitle }}
        </text>
      </view>
    </scroll-view>
    <NoDataPage v-else :status="0" />
  </view>
  <NoDataPage v-else :status="1" />
  <ContactTeacher v-model:show-overlay="showTeacher" :type="3" />
</template>

<script lang="ts" setup>
import { getCollectionList } from '@/api/collection/index';
import type { GetCollectionListResult } from '@/api/collection/type';
import type { ListPageParams } from '@/api/read/type';
import { useUserStore } from '@/store';
import { isLogin } from '@/utils/auth';

const store = useUserStore();
const loginFlag = ref<boolean>(isLogin());
const hasSchool = computed(() => !!store.schoolId); // 是否绑定门店

const tabIndex = ref<number>(2);
const pageParams = ref<ListPageParams>({
  pageNum: 1,
  pageSize: 10,
});
const total = ref<number>(0);
const collectList = ref<Array<GetCollectionListResult>>([]);
const getList = () => {
  getCollectionList({ type: tabIndex.value, ...pageParams.value }).then((res: { rows: GetCollectionListResult; total: number }) => {
    collectList.value = collectList.value.concat(res.rows);
    total.value = res.total;
  });
};

const hancldChange = (index: number) => {
  tabIndex.value = index;
  pageParams.value.pageNum = 1;
  collectList.value = [];
  getList();
};

const scrolltolower = () => {
  if (total.value <= collectList.value.length)
    return;
  pageParams.value.pageNum++;
  getList();
};

const showTeacher = ref<boolean>(false);
const toDetail = (item: GetCollectionListResult) => {
  if (item.vipFlag === '1') { // vip数据
    if (!hasSchool.value) { // 未绑定门店
      showTeacher.value = true;
      return;
    }
    if ([0, 3].includes(store.vip.flag)) { // 非vip
      uni.$u.toast('VIP未生效！');
      return;
    }
    if (store.vip.flag === 2) { // vip过期
      uni.$u.toast('VIP已过期！');
      return;
    }
  }
  uni.navigateTo({
    url: `/pages/views/microClassroom/detail?id=${item.courseId}&type=${tabIndex.value}&vipFlag=${item.vipFlag}`,
  });
};

onLoad(() => {
  if (loginFlag.value)
    getList();
});

// 分享
uni.showShareMenu({
  withShareTicket: true,
  menus: ['shareAppMessage', 'shareTimeline'],
});
onShareAppMessage(() => {
  return {
    title: store.share_txt,
    imageUrl: store.share_pic_url,
  };
});
</script>

<style lang="scss" scoped>
.collect-page {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 24rpx 40rpx 0;

  .collect-tab {
    display: flex;
    margin-bottom: 40rpx;

    .tab-item {
      padding-bottom: 8rpx;
      font-size: 32rpx;
      color: #000000;
      line-height: 48rpx;
    }

    .tab-right {
      margin-left: 64rpx;
    }

    .active {
      font-weight: bold;
      color: #0BC196;
      border-bottom: 4rpx solid #0BC196;
    }
  }

  .collect-list {
    flex: 1;
    overflow: auto;

    .collect-item {
      display: flex;
      align-items: center;
      padding: 28rpx 0;
      border-bottom: 2rpx solid #EDEDED;

      .item-img {
        width: 200rpx;
        height: 113rpx;
        border-radius: 8rpx;
      }

      .item-title {
        flex: 1;
        padding-left: 24rpx;
        font-weight: bold;
        font-size: 28rpx;
        color: #251504;
        line-height: 42rpx;
      }

      &::marker {
        content: '';
        width: 0 !important;
        height: 0 !important;
      }
    }
    .collect-item:first-child {
      padding-top: 0;
    }
    .collect-item:last-child {
      margin-bottom: 40rpx;
    }
  }

  no-data-page {
    flex: 1;
  }
}
</style>
